<?php

namespace app\api\controller\system;
use app\BaseController;
use app\api\service\system\AdminUserService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\util\ResponseUtil;
use app\util\UserUtil;

class AdminUser extends BaseController
{
    public function getAdminUserInfo()
    {
        $id = $this->requestData['id'];
        $service = new AdminUserService();
        $finalData = $service->getAdminUserInfoById($id);
        return ResponseUtil::success($finalData);
    }

    public function getAdminUserList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new AdminUserService();
        $list = $service->getAdminUserList($this->requestData, $pageIndex, $pageSize);
        $total = $service->getAdminUserTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getAdminUserOption()
    {
        $service = new AdminUserService();
        $list = $service->getAdminUserOption($this->requestData);
        return ResponseUtil::success($list);
    }

    public function createAdminUser()
    {
        try{
            if(empty($this->requestData['page_code'])){
                return ResponseUtil::failure("页面标识码不可为空");
            }
            $pageCode = $this->requestData['page_code'];

            $serviceDatagridColumn = new DatagridColumnService();
            $columnList = $serviceDatagridColumn->getDatagridColumnOption(['page_code' => $pageCode, 'is_in_add'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }
            $service = new AdminUserService();
            $res = $service->addAdminUser($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($res, ['##field_1##' => '##field_text_1##', '##field_2##'=>'##field_text_2##']);
                $messageCn = '新增任务项,'.$addInfo;
                $messageEn = '';
                $logData = [
                    'subject_id' => $res['new_id'],
                    'remark'  => $res['task_item_no'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,

                ];
                UserUtil::addOperateLog('AdminUser', UserOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增失败");
        }
    }

    public function modifyAdminUser()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的任务");
            }
            if(empty($this->requestData['##field_1##'])){
                return ResponseUtil::failure("请填写##field_text_1##");
            }
            if(empty($this->requestData['##field_2##'])){
                return ResponseUtil::failure("请填写##field_text_2##");
            }
            $id = $this->requestData['id'];
            $service = new AdminUserService();
            $oldInfo = $service->getAdminUserInfoById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("任务不存在");
            }
            $res = $service->editAdminUserById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData,['##field_1##' => '##field_text_1##', '##field_2##'=>'##field_text_2##', 'status'=>'状态']);
                $messageCn = '编辑任务项,'.$changeInfo['update_info'];
                $messageEn = '';
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['##field_1##'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('AdminUser', UserOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑失败");
        }
    }

    public function trashAdminUser()
    {
        try{
            $id = $this->requestData['id'];
            $service = new AdminUserService();
            $info = $service->getAdminUserInfoById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->deleteAdminUserById($id);
            if($res){
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }

    public function deleteMultiAdminUser(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new AdminUserService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->getAdminUserInfoById($value);
                if(!$info){
                    return ResponseUtil::failure("数据不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->deleteAdminUserByManyId($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($deleteData, ['##field_1##', '##field_2##']);
                $messageCn = '删除任务项,'.$deleteInfo;
                $messageEn = '';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => implode(',', array_column($deleteData, '##field_1##')),
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('AdminUser', UserOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }

    public function toggleAdminUser()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的数据");
            }

            $arrField = ['is_in_add', 'is_in_edit', 'is_require', 'is_hide', 'status'];
            $arrFieldTitle = ['is_in_add' => '新增是否包含', 'is_in_edit'=>'编辑是否包含', 'is_hide'=>'是否隐藏', 'status'=>'状态'];
            if(empty($this->requestData['field_name'])){
                return ResponseUtil::failure("请指定要编辑的列");
            }

            $id = $this->requestData['id'];
            $fieldName = $this->requestData['field_name'];
            if(!in_array($fieldName, $arrField)){
                return ResponseUtil::failure("不支持编辑当前列");
            }

            $service = new AdminUserService();
            $oldInfo = $service->getAdminUserInfoById($id);

            $updateData[$fieldName] = empty($this->requestData['value']) ? 2:$this->requestData['value'];
            $res = $service->editAdminUserById($id, $updateData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $updateData,$arrFieldTitle);
                $messageCn = '编辑页面纵列,'.$changeInfo['update_info'];
                $messageEn = '';
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['column_name'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('AdminUser', UserOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("更新失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("更新失败");
        }
    }

    public function simpleUpdateAdminUser()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的数据");
            }

            $id = $this->requestData['id'];
            $arrFieldTitle = ['sort' => '排序',  'status'=>'状态'];
            $service = new AdminUserService();
            $oldInfo = $service->getAdminUserInfoById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->editAdminUserById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $arrFieldTitle);
                $messageCn = '编辑页面纵列,'.$changeInfo['update_info'];
                $messageEn = '';
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['column_name'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('AdminUser', UserOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑失败");
        }
    }

    public function logout()
    {
        try {
            unset($_SESSION['admin_login_info']);
            return ResponseUtil::success();
        } catch (\Exception $e) {
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("登出失败");
        }
    }



}



